package design.patterns.observer.simpleobserver;

import java.util.Enumeration;
import java.util.Vector;

/**
 * @Desc:   具体观察者对象，维护一个观察者列表，并且通知所有观察者，在事件产生时
 * @author: cww
 * @DateTime: 2020-06-11 23:49
 */

public class ConcreteSubject implements Subject {

    private Vector<Observer> observers = new Vector<>();
    @Override
    public void attach(Observer observer) {
        observers.addElement(observer);
    }

    @Override
    public void detach(Observer observer) {
        observers.removeElement(observer);
    }

    @Override
    public void notifyObservers() {
        Enumeration<Observer> enumeration = observers();
        while (enumeration.hasMoreElements()) {
            enumeration.nextElement().update();
        }
    }

    public Enumeration<Observer> observers(){
        return observers.elements();
    }
}
